﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Common.Library;
using OnlineVideos.UI;
using OnlineVideos.Library;
using OnlineVideos.Common;
using OnlineVideos.Data;
using System.ComponentModel;
using Common.Utilities;
using OnlineVideos.Entities;
//using CommonControls.ClassFiles;

namespace OnlineVideos.Views
{
    public partial class CategoryDetails : PhoneApplicationPage
    {
        #region GlobalDeclaration
        int recentCount;
        int teluguCount;
        int tamilCount;
        #endregion

        #region Constructor
        public CategoryDetails()
        {
            InitializeComponent();
            LoadPivotThemes();
            teluguCount = 10;
            tamilCount = 10;
            recentCount = 10;
        }
        #endregion
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            FlurryWP8SDK.Api.EndTimedEvent("CategoryDetails Page");
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            FlurryWP8SDK.Api.LogPageView();
        }
        #region PageLoad
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("CategoryDetails Page", true);
                string CategoryName = string.Empty;
                string CategoryID = string.Empty;
                if(NavigationContext.QueryString.TryGetValue("catid", out CategoryID) 
                    && NavigationContext.QueryString.TryGetValue("catname", out CategoryName))
                {
                    if (CategoryName == "all subjects")
                        AppSettings.AllSubjects = CategoryName;
                    if(CategoryName=="all recipes")
                        AppSettings.AllRecipes = CategoryName;
                    AppSettings.CategoryID = CategoryID;
                    tblkVideosTitle.Text = CategoryName.ToUpper();
                    BackgroundHelper bwHelper = new BackgroundHelper();
                    if (AppSettings.ProjectName == "Indian Cinema" || AppSettings.ProjectName == "Indian Cinema Pro")
                    {
                        bwHelper.AddBackgroundTask(
                                                  (object s, DoWorkEventArgs a) =>
                                                  {
                                                      a.Result = OnlineShow.GetLanguages(recentCount, CategoryID,"Hindi");
                                                  },
                                                  (object s, RunWorkerCompletedEventArgs a) =>
                                                  {
                                                      lbxCatory.ItemsSource = (List<ShowList>)a.Result;
                                                  }
                                                );
                        bwHelper.AddBackgroundTask(
                                                 (object s, DoWorkEventArgs a) =>
                                                 {
                                                     a.Result = OnlineShow.GetLanguages(teluguCount, CategoryID, "telugu");
                                                 },
                                                 (object s, RunWorkerCompletedEventArgs a) =>
                                                 {
                                                     lbxTopRated.ItemsSource = (List<ShowList>)a.Result;
                                                 }
                                               );
                        bwHelper.AddBackgroundTask(
                                                 (object s, DoWorkEventArgs a) =>
                                                 {
                                                     a.Result = OnlineShow.GetLanguages(tamilCount, CategoryID, "tamil");
                                                 },
                                                 (object s, RunWorkerCompletedEventArgs a) =>
                                                 {
                                                     lbxRecentlyAdded.ItemsSource = (List<ShowList>)a.Result;
                                                 }
                                               );
                    }
                    else
                    {
                        bwHelper.AddBackgroundTask(
                                                    (object s, DoWorkEventArgs a) =>
                                                    {
                                                        a.Result = OnlineShow.LoadCategoryTopRated(recentCount, CategoryID);
                                                    },
                                                    (object s, RunWorkerCompletedEventArgs a) =>
                                                    {
                                                        lbxCatory.ItemsSource = (List<ShowList>)a.Result;
                                                    }
                                                  );
                    }
                    bwHelper.RunBackgroundWorkers();
                }
               
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In CategoryDetails file.", ex);
            }
        }
        #endregion

        #region "Common Methods"
        private void LoadPivotThemes()
        {
            if (AppSettings.ProjectName == "Indian Cinema" || AppSettings.ProjectName == "Indian Cinema Pro")
            {
                pvtcat.Header = "hindi";
                pvttelugu.Header = "telugu";
                pvttamil.Header = "tamil";
                pvttelugu.Visibility = Visibility.Visible;
                pvttamil.Visibility = Visibility.Visible;
                //pvtupcoming.Header = "upcoming movies";
                //pvtupcoming.Visibility = Visibility.Visible;
            }
            else
            {
                pvtcat.Header = "";
                pvttelugu.Visibility = Visibility.Collapsed;
                pvttamil.Visibility = Visibility.Collapsed;
                pvtVideos.Items.Remove(pvttamil);
                //pvtupcoming.Visibility = Visibility.Collapsed;
                pvtVideos.Items.Remove(pvttelugu);
            }
        }
        #endregion

        #region Events
        private void lbxCatory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
               
                if (lbxCatory.SelectedIndex != -1)
                {
                   
                    string title = (lbxCatory.SelectedItem as ShowList).Title;
                    AppSettings.ShowID = (lbxCatory.SelectedItem as ShowList).ShowID.ToString();
                    AppState.searchtitle = string.Empty;
                    if (title == "get more")
                    {
                        Constants.UIThread = true;
                        recentCount = recentCount + 10;
                        if (AppSettings.ProjectName == "Indian Cinema" || AppSettings.ProjectName == "Indian Cinema Pro")
                        {
                            lbxCatory.ItemsSource = OnlineShow.GetLanguages(recentCount, AppSettings.CategoryID,"Hindi");
                        }
                        else
                        lbxCatory.ItemsSource = OnlineShow.LoadCategoryTopRated(recentCount, AppSettings.CategoryID);
                        lbxCatory.ScrollIntoView(lbxCatory.Items[recentCount - 10]);
                    }
                    else
                        PageHelper.NavigateToDetailPage(AppResources.DetailPageName, AppSettings.ShowID);
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxCatory_SelectionChanged Method In CategoryDetails file.", ex);
            }
            lbxCatory.SelectedIndex = -1;
        }
        private void imgTitle_MouseEnter(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        #endregion

        private void lbxTopRated_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                if (lbxTopRated.SelectedIndex != -1)
                {

                    string title = (lbxTopRated.SelectedItem as ShowList).Title;
                    AppSettings.ShowID = (lbxTopRated.SelectedItem as ShowList).ShowID.ToString();
                    AppState.searchtitle = string.Empty;
                    if (title == "get more")
                    {
                        Constants.UIThread = true;
                        teluguCount = teluguCount + 10;
                        lbxTopRated.ItemsSource = OnlineShow.GetLanguages(teluguCount, AppSettings.CategoryID, "telugu");
                        lbxTopRated.ScrollIntoView(lbxTopRated.Items[teluguCount - 10]);
                    }
                    else
                        PageHelper.NavigateToDetailPage(AppResources.DetailPageName, AppSettings.ShowID);
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxTopRated_SelectionChanged Method In CategoryDetails file.", ex);
            }
            lbxTopRated.SelectedIndex = -1;
        }

        private void lbxRecentlyAdded_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                if (lbxRecentlyAdded.SelectedIndex != -1)
                {

                    string title = (lbxRecentlyAdded.SelectedItem as ShowList).Title;
                    AppSettings.ShowID = (lbxRecentlyAdded.SelectedItem as ShowList).ShowID.ToString();
                    AppState.searchtitle = string.Empty;
                    if (title == "get more")
                    {
                        Constants.UIThread = true;
                        tamilCount = tamilCount + 10;
                        lbxRecentlyAdded.ItemsSource = OnlineShow.GetLanguages(tamilCount, AppSettings.CategoryID, "tamil");
                        lbxRecentlyAdded.ScrollIntoView(lbxRecentlyAdded.Items[tamilCount - 10]);
                    }
                    else
                        PageHelper.NavigateToDetailPage(AppResources.DetailPageName, AppSettings.ShowID);
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxRecentlyAdded_SelectionChanged Method In CategoryDetails file.", ex);
            }
            lbxRecentlyAdded.SelectedIndex = -1;
        }
    }
}